package com.ruoyi.web.controller.order;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.hx.domain.Goods;
import com.ruoyi.hx.domain.OrderDetail;
import com.ruoyi.hx.service.IOrderDetailService;
import com.ruoyi.system.domain.SysDictData;
import com.ruoyi.system.utils.DictUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单详情Controller
 * 
 * @author whx
 * @date 2020-11-20
 */
@Controller
@RequestMapping("/hx/detail")
public class OrderDetailController extends BaseController
{
    private String prefix = "hx/detail";

    @Autowired
    private IOrderDetailService orderDetailService;

    @RequiresPermissions("hx:detail:view")
    @GetMapping()
    public String detail()
    {
        return prefix + "/detail";
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  20:59  2020/11/28
     * @Version:  1.0.0
     * @Description:  获取缓存中的字典数据
     */

    @ResponseBody
    @PostMapping("/getDict")
    /*采用两种方式获取缓存中的数据，我们用map集合进行封装 */
    public Map getDict(String dataType)
    {
        List<SysDictData> dictCache = DictUtils.getDictCache(dataType);

        String cacheKey = DictUtils.getCacheKey(dataType);
        /*阿里公约中规定，最好吧map的初始化容量加上，
        map中数据变化的时候有一个扩容的过程 有个值叫0.75（四分之三）
        就是达到这个值得时候，map就会进行一次扩容，扩容消耗是很大的
        所以如果知道要存的数量，最后初始化时写上比要存数量稍微大一点的数，
        且这个初始的容量最好是 2的次幂*/
        Map map = new HashMap(4);
        map.put("chcheKey",cacheKey);
        map.put("dictCache",dictCache);

        return map;
    }

    /**
     * 查询订单详情列表
     */
    @RequiresPermissions("hx:detail:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OrderDetail orderDetail)
    {
        startPage();
        List<OrderDetail> list = orderDetailService.selectOrderDetailList(orderDetail);
        return getDataTable(list);
    }

    /**
     * 查询订单详情列表 --无分页
     */
    @RequiresPermissions("hx:detail:list")
    @PostMapping("/listNoPage")
    @ResponseBody
    public List<OrderDetail> listNoPage(OrderDetail orderDetail)
    {
        return orderDetailService.selectOrderDetailList(orderDetail);
    }

    /**
     * 导出订单详情列表
     */
    @RequiresPermissions("hx:detail:export")
    @Log(title = "订单详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OrderDetail orderDetail)
    {
        List<OrderDetail> list = orderDetailService.selectOrderDetailList(orderDetail);
        ExcelUtil<OrderDetail> util = new ExcelUtil<OrderDetail>(OrderDetail.class);
        return util.exportExcel(list, "detail");
    }

    /**
     * 新增订单详情
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存订单详情
     */
    @RequiresPermissions("hx:detail:add")
    @Log(title = "订单详情", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(OrderDetail orderDetail)
    {
        return toAjax(orderDetailService.insertOrderDetail(orderDetail));
    }

    /**
     * 提交生成--订单详情
     */
    @RequiresPermissions("hx:detail:add")
    @Log(title = "订单详情", businessType = BusinessType.INSERT)
    @PostMapping("/saveOrder")
    @ResponseBody
    public AjaxResult saveOrder(@RequestBody List<OrderDetail>  orderDetail)
    {
        try {
            orderDetailService.saveOrder(orderDetail, ShiroUtils.getLoginName());
            return AjaxResult.success("成功");
        }catch (Exception e){
            return  AjaxResult.error("操作失败");
        }
    }

    /**
     * 修改订单详情
     */
    @GetMapping("/edit/{goodsId}")
    public String edit(@PathVariable("goodsId") Long goodsId, ModelMap mmap)
    {
        OrderDetail orderDetail = orderDetailService.selectOrderDetailById(goodsId);
        mmap.put("orderDetail", orderDetail);
        return prefix + "/edit";
    }

    /**
     * 修改保存订单详情
     */
    @RequiresPermissions("hx:detail:edit")
    @Log(title = "订单详情", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OrderDetail orderDetail)
    {
        return toAjax(orderDetailService.updateOrderDetail(orderDetail));
    }

    /**
     * 删除订单详情
     */
    @RequiresPermissions("hx:detail:remove")
    @Log(title = "订单详情", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(orderDetailService.deleteOrderDetailByIds(ids));
    }
}
